package pers.qianyu.month_202101.date_20210126;

import java.util.*;

/**
 * 1128. 等价多米诺骨牌对的数量
 * https://leetcode-cn.com/problems/number-of-equivalent-domino-pairs/
 *
 * @author mizzle rain
 * @date 2021-01-26 16:51
 */
public class NumEquivDominoPairs {
    public int numEquivDominoPairs(int[][] dominoes) {
        HashMap<String, Integer> map = new HashMap<>();
        int res = 0;
        for (int[] d : dominoes) {
            String key = d[0] > d[1] ? d[1] + "" + d[0] : d[0] + "" + d[1];
            map.put(key, map.getOrDefault(key, 0) + 1);
            if (map.get(key) > 2) res += map.get(key) - 1;
            else if (map.get(key) == 2) res++;
        }
        return res;
    }

    // 简化哈希计数
    public int numEquivDominoPairs2(int[][] dominoes) {
        int[] map = new int[100];
        int res = 0;
        for (int[] d : dominoes) {
            int t = d[0] > d[1] ? d[1] * 10 + d[0] : d[0] * 10 + d[1];
            map[t]++;
            if (map[t] == 2) res++;
            else if (map[t] > 2) res += map[t] - 1;
        }
        return res;
    }
}
